package com.lyhengtongwl.zqsnews.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.flyco.tablayout.SlidingTabLayout;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.adapter.PagerAdapter;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.ui.fragment.MyorderFragment;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的订单
 */
public class MyorderActivity extends BaseActivity {

    private List<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"全部", "待支付", "待收货", "已完成"};
    private int status = -1;//订单状态('订单类型：-1.全部；0.待支付；1.待收货；2.已完成；)

    @BindView(R.id.stl)
    SlidingTabLayout stl;
    @BindView(R.id.vp)
    ViewPager vp;

    @Override
    public void setListen() {

    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("我的订单")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_myorder;
    }

    @Override
    public void initView() {

        mFragments.add(MyorderFragment.newInstance("全部", "-1"));
        mFragments.add(MyorderFragment.newInstance("待支付", "0"));
        mFragments.add(MyorderFragment.newInstance("待收货", "1"));
        mFragments.add(MyorderFragment.newInstance("已完成", "2"));
        vp.setAdapter(new PagerAdapter(getSupportFragmentManager(), mFragments, mTitles));
        stl.setViewPager(vp);
        stl.setBackgroundColor(getResources().getColor(R.color.white));
        status = getIntent().getIntExtra("status", -1);
        if (status == -1) {//全部
            stl.setCurrentTab(0);
        } else if (status == 0) {
            stl.setCurrentTab(1);
        } else if (status == 1) {
            stl.setCurrentTab(2);
        } else if (status == 2) {
            stl.setCurrentTab(3);
        }

    }

    @Override
    public void initData() {

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        int pos = vp.getCurrentItem();
        switch (pos) {
            case 0://全部列表
                if (mFragments.get(0) != null) {
                    ((MyorderFragment) mFragments.get(0)).refreshData();
                }
                break;
            case 1://待支付
                if (mFragments.get(1) != null) {
                    ((MyorderFragment) mFragments.get(1)).refreshData();
                }
                break;
            case 3://已完成
                if (mFragments.get(3) != null) {
                    ((MyorderFragment) mFragments.get(3)).refreshData();
                }
                break;
        }


    }
}
